package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.MchntAuthCtrlMapper;
import cc.rengu.igas.bomp.common.entity.MchntAuthCtrl;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户终端业务权限控制表操作接口实现类
 * Created by 王成 on 2018/5/25.
 */
public class MchntAuthCtrlMapperImpl implements MchntAuthCtrlMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int inertMchntAuthCtrlInfo(MchntAuthCtrl mchntAuthCtrl) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "inertMchntAuthCtrlInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntAuthCtrl, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户业务权限控制信息失败,instId:<{}>,ctrlType:<{}>,mchntNo:<{}>,termNo:<{}>,txnNum:<{}>,bizType:<{}>,id:<{}>,iReturnCode:<{}>",
                    mchntAuthCtrl.getInstId(), mchntAuthCtrl.getCtrlType(), mchntAuthCtrl.getMchntNo(), mchntAuthCtrl.getTermNo(), mchntAuthCtrl.getTxnNum(), mchntAuthCtrl.getBizType(), mchntAuthCtrl.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public MchntAuthCtrl selectMchntAuthCtrlInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntAuthCtrlInfoById");
        MchntAuthCtrl mchntAuthCtrl = new MchntAuthCtrl();
        mchntAuthCtrl.setInstId(instId);
        mchntAuthCtrl.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntAuthCtrl, mchntAuthCtrl);
        if (0 != iReturnCode) {
            rglog.error("获取商户业务权限控制信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return mchntAuthCtrl;
    }

    @Override
    public int updateMchntAuthCtrlInfoById(MchntAuthCtrl mchntAuthCtrl) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntAuthCtrlInfoById");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntAuthCtrl, null);
        if (0 != iReturnCode) {
            rglog.error("根据非业务主键更新商户业务权限控制信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", mchntAuthCtrl.getInstId(), mchntAuthCtrl.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public MchntAuthCtrl selectMchntAuthCtrlInfoByPrimaryKey(String instId, String ctrlType, String mchntNo, String termNo, String txnNum, String bizType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntAuthCtrlInfoByPrimaryKey");
        MchntAuthCtrl mchntAuthCtrl = new MchntAuthCtrl();
        mchntAuthCtrl.setInstId(instId);
        mchntAuthCtrl.setCtrlType(ctrlType);
        mchntAuthCtrl.setMchntNo(mchntNo);
        mchntAuthCtrl.setTermNo(termNo);
        mchntAuthCtrl.setTxnNum(txnNum);
        mchntAuthCtrl.setBizType(bizType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntAuthCtrl, mchntAuthCtrl);
        if (0 != iReturnCode) {
            rglog.error("获取商户业务权限控制信息失败,instId:<{}>,ctrlType:<{}>,mchntNo:<{}>,termNo:<{}>,txnNum:<{}>,bizType:<{}>,iReturnCode:<{}>", instId, ctrlType, mchntNo, termNo, txnNum, bizType, iReturnCode);
            return null;
        }
        return mchntAuthCtrl;
    }

    @Override
    public List<MchntAuthCtrl> selectMchntAuthCtrlInfoByCtrlInst(String instId, String ctrlType, String mchntNo, String termNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntAuthCtrlInfoByCtrlInst");
        MchntAuthCtrl mchntAuthCtrl = new MchntAuthCtrl();
        mchntAuthCtrl.setInstId(instId);
        mchntAuthCtrl.setCtrlType(ctrlType);
        mchntAuthCtrl.setMchntNo(mchntNo);
        mchntAuthCtrl.setTermNo(termNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntAuthCtrl, MchntAuthCtrl.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据控制主体获取业务权限控制信息失败,instId:<{}>,ctrlType:<{}>,mchntNo:<{}>,termNo:<{}>", instId, ctrlType, mchntNo, termNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntAuthCtrl) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntAuthCtrl> selectMchntAuthCtrlInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntAuthCtrlInfoByMchntNo");
        MchntAuthCtrl mchntAuthCtrl = new MchntAuthCtrl();
        mchntAuthCtrl.setInstId(instId);
        mchntAuthCtrl.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntAuthCtrl, MchntAuthCtrl.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据控制主体获取业务权限控制信息失败,instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntAuthCtrl) item).collect(Collectors.toList());
    }


    @Override
    public List<MchntAuthCtrl> selectMchntAuthCtrlInfoByBizProdCode(String instId, String mchntNo, String bizProdCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntAuthCtrlInfoByBizProdCode");
        MchntAuthCtrl mchntAuthCtrl = new MchntAuthCtrl();
        mchntAuthCtrl.setInstId(instId);
        mchntAuthCtrl.setMchntNo(mchntNo);
        mchntAuthCtrl.setBizProdCode(bizProdCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntAuthCtrl, MchntAuthCtrl.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据产品编号获取业务权限控制信息失败,instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntAuthCtrl) item).collect(Collectors.toList());
    }
}
